﻿#include "EndOfChargeVerify.h"
#include "ui_endofchargeverify.h"

EndOfChargeVerify::EndOfChargeVerify(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EndOfChargeVerify)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    ui->labelIdentifyCode->hide();
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        countdown[i] = new CountDownTime(ui->frameMid);
        countdown[i]->setGeometry(710, 10, 30, 40);
        countdown[i]->mysetText("60");
        connect(countdown[i], SIGNAL(CoundDownTimeisOver()), this, SLOT(countdownOver()));
    }
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(setCountHide(int)));
    keyBoardConnect();
}

EndOfChargeVerify::~EndOfChargeVerify()
{
    delete ui;
}

void EndOfChargeVerify::setCountHide(int current)
{
    if(current == endOfChargeVerifyPage)
    {
        ui->_focus_gun_->setText(QString::number(focus_gun_));
        ui->lineEdit->clear();
        if(!countdown[focus_gun_-1]->OneSecound->isActive())
            countdown[focus_gun_-1]->CountDownStart();
        for(int i=0; i<GUN_AMOUNT; i++)
        {
            if(focus_gun_-1 != i)
            {
                countdown[i]->hide();
            }
            else
            {
                countdown[i]->show();
            }
        }
    }
}

void EndOfChargeVerify::countdownOver()
{
    /*待完成*/
    CountDownTime* sender_ = static_cast<CountDownTime*>(sender());
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        if(sender_ == countdown[i])
        {
            on_buttonBack_clicked();
        }
    }
}
void EndOfChargeVerify::on_buttonOk_clicked()
{
    /*待完成*/
    QString Code;
    QString passwrd;
    if( newPassword.restore() )
    {
        passwrd = newPassword.password();
    }
    else
    {
        passwrd = "123456";
    }

    Code = chargingData->cmd3[focus_gun_ - 1].stopcode;
    if(Code.trimmed() == ui->lineEdit->text() || ui->lineEdit->text() == passwrd)
    {
//        chargingData->cmd4[focus_gun_ - 1].guntab = focus_gun_ ;
        chargingData->cmd4[focus_gun_ - 1].guntab = focus_gun_;
        chargingData->cmd4[focus_gun_ - 1].req_state = 1;
        GUN_STATE[focus_gun_ - 1].Page = chargeWatchingDCPage;
        ui->lineEdit->clear();
    }
    else
    {
        QTimer::singleShot(8000,ui->labelIdentifyCode,SLOT(hide()));//显示8秒后隐藏
        ui->lineEdit->clear();
    }
}
void EndOfChargeVerify::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();

    keyHandle(btn, focus);

}

void EndOfChargeVerify::keyHandle(QPushButton* btn, QLineEdit* lineEdit)
{
    if( btn->text() == tr("空格") )
    {
        lineEdit->insert(" ");
    }
    else if( btn->text() == tr("退格") )
    {
        lineEdit->backspace();
    }
    else if( btn->text() == tr("清除"))
    {
        lineEdit->clear();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}

void EndOfChargeVerify::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Space, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
}



void EndOfChargeVerify::on_buttonBack_clicked()
{
    countdown[focus_gun_ - 1]->CountDownStop();
    GUN_STATE[focus_gun_ - 1].Page = chargeWatchingDCPage;
    ui->lineEdit->clear();
}
